<template>
  <view>
    <view class="search-box" style="background-color: #c00000;"><uni-search-bar @input="input" radius="100" cancelButton="none" focus></uni-search-bar></view>
    <!-- 搜索建议列表 -->
    <uni-list v-if="searchResults.length !== 0">
      <uni-list-item
        showArrow
        :title="item.goods_name"
        v-for="(item, index) in searchResults"
        :key="index"
        ellipsis="1"
        :to="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`"
      ></uni-list-item>
    </uni-list>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clear"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list"><uni-tag :text="item" v-for="(item, i) in history" :key="i" @click="gotoGoodsList(item)"></uni-tag></view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'SearchPage',
  components: {},
  data() {
    return {
      timer: null,
      kw: '',
      searchResults: [],
      historyList: []
    };
  },
  created() {},
  mounted() {},
  onLoad() {
    this.historyList = uni.getStorageSync('kw') || []
  },
  computed: {
    history() {
      return [...this.historyList].reverse();
    }
  },
  methods: {
    input(e) {
      if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
      }
      this.timer = setTimeout(() => {
        this.kw = e;
        this.getSearchResults();
      }, 500);
    },
    async getSearchResults() {
      if (this.kw === '') {
        this.searchResults = [];
        return;
      }
      let { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw });
      console.log(res);
      if (res.meta.status !== 200) return uni.$showMsg();
      this.searchResults = res.message;
      this.saveSearchHistory();
    },
    saveSearchHistory() {
      // this.historyList.push(this.kw)
      // 1. 将 Array 数组转化为 Set 对象
      const set = new Set(this.historyList);
      // 2. 调用 Set 对象的 delete 方法，移除对应的元素
      set.delete(this.kw);
      // 3. 调用 Set 对象的 add 方法，向 Set 中添加元素
      set.add(this.kw);
      // 4. 将 Set 对象转化为 Array 数组
      this.historyList = Array.from(set);
      // 调用 uni.setStorageSync(key, value) 将搜索历史记录持久化存储到本地
      uni.setStorageSync('kw', this.historyList)
    },
    clear() {
      this.historyList = []
      uni.setStorageSync('kw', [])
    },
    gotoGoodsList(item) {
      console.log(item);
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list?query=' + item
      })
    }
  }
};
</script>

<style lang="scss">
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
.history-box {
  padding: 0 5px;

  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }

  .history-list {
    display: flex;
    flex-wrap: wrap;

    .uni-tag {
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}
</style>
